package com.haisen.controller;

import com.alibaba.fastjson.JSON;
import com.haisen.core.annotation.SystemLog;
import com.haisen.core.utils.PageResultSet;
import com.haisen.core.utils.Result;
import com.haisen.dataobject.entity.Area;
import com.haisen.dataobject.entity.Item;
import com.haisen.dataobject.query.ItemQuery;
import com.haisen.service.AreaService;
import com.haisen.service.ItemService;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;

@Controller
@RequestMapping("/area")
@Slf4j
public class AreaController {

    @Autowired
    private AreaService areaService;


    //todo:地区关系读取

    @GetMapping
    @RequiresPermissions("area:view")
    public String page(Model model) {
        List<Area> areaList = areaService.findAll();
       // Area[] areaArray = areas.toArray(new Area[areas.size()]);
        model.addAttribute("areaList",areaList);
        return "system/area";
    }


    /**
     * 二级联动菜单，查父id下的所有子节点
     * @param pid
     * @return
     */
    @ResponseBody
    @RequestMapping("/cityCountry")
    @RequiresPermissions("area:view")
    public Result getCityCountry(@RequestParam("pid") Long pid) {
        return Result.success(areaService.getCityCountry( pid));
    }


    @ResponseBody
    @RequestMapping("/cityCountryByname")
    @RequiresPermissions("area:view")
    public Result getCityCountry(@RequestParam("name") String name) {
        return Result.success(areaService.getCityCountry(name));
    }


    @ResponseBody
    @PostMapping("/create")
    @RequiresPermissions("area:create")
    @SystemLog("组织管理添加区域")
    public Result create(@Valid Area area) {
      //  log.info("传递对象={}", JSON.toJSONString(area));
         areaService.create(area);
        return Result.success();
    }



    @ResponseBody
    @PostMapping("/update")
    @RequiresPermissions("area:update")
    @SystemLog("组织管理修改区域")
    public Result update(Area  area) {
        areaService.update(area);
        return Result.success();
    }

    @ResponseBody
    @PostMapping("/delete")
    @RequiresPermissions("area:delete")
    @SystemLog("组织管理删除区域")
    public Result delete(@RequestParam("id") Long[] ids) {
        Arrays.asList(ids).forEach(id -> areaService.delete(id));
        return Result.success();
    }


}
